package com.iamback.dy.examine.controller.admin.examine;

import java.util.Calendar;
import java.util.Date; 

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamback.dy.entity.DyUser;
import com.iamback.dy.entity.DyUserInfo;
import com.iamback.dy.entity.DyuserBlocked;
import com.iamback.dy.entity.User;
import com.iamback.dy.examine.common.JsonResult;
import com.iamback.dy.examine.controller.modle.BlockedDyUserVo;
import com.iamback.dy.examine.controller.modle.BlockedView;
import com.iamback.dy.examine.service.ICommentExuserService;
import com.iamback.dy.examine.service.IContentExuserService;
import com.iamback.dy.examine.service.IContentUnreviewedService;
import com.iamback.dy.examine.service.IDyContentCommentService;
import com.iamback.dy.examine.service.IDyUserInfoService;
import com.iamback.dy.examine.service.IDyUserService;
import com.iamback.dy.examine.service.IDyuserBlockedService;

@Controller
@RequestMapping("/examine/dy/user")
public class ExamineDyUserController {

	@Autowired
	private IDyUserService dyUserService;
	@Autowired
	private IDyUserInfoService dyUserInfoService;
	@Autowired
	private IContentUnreviewedService contentUnreviewedService;
	@Autowired
	private IContentExuserService contentExuserService;
	@Autowired
	private ICommentExuserService commentExuserService;
	@Autowired
	private IDyContentCommentService contentCommentService;
	@Autowired
	private IDyuserBlockedService blockedService;

	@RequestMapping(value = "/blocked/info")
	@ResponseBody
	public JsonResult getBlockedDyUserInfo(String publisherDyId) {
		User userId = (User) SecurityUtils.getSubject().getSession().getAttribute("loginUserInfo");
		if (userId == null || userId.getId() == null) {
			return JsonResult.failure("参数错误");
		}
		if (publisherDyId != null && !"".equals(publisherDyId.trim())) {
			// 用户信息
			 DyUser dyUser =dyUserService.findDyUserByDyId(publisherDyId);
			DyUserInfo dyUserInfo = dyUserInfoService.findDyUserInfoById(publisherDyId.trim());
			if (dyUserInfo != null) {
				// 帖子统计信息
				Integer countSum = contentUnreviewedService.countTakeByDyId(publisherDyId.trim());
				Integer countPass = contentUnreviewedService.countTakePassByDyId(publisherDyId.trim());

				BlockedView blockedView = new BlockedView();
				blockedView.setPublisherDyId(publisherDyId);
				blockedView.setContentCount(countSum);
				blockedView.setCreateDate(dyUser.getCreateData());
				blockedView.setContentPassCount(countPass);
				blockedView.setHeadPortraitUrl(dyUserInfo.getHeadPortraitUrl());
				blockedView.setNickName(dyUserInfo.getNickName());
				blockedView.setSignature(dyUserInfo.getSignature());
				return JsonResult.success("成功", blockedView);
			} else {
				return JsonResult.failure("获取数据失败");
			}
		} else {
			return JsonResult.failure("参数错误");
		}

	}

	@RequestMapping(value = "/blocked/info/update")
	@ResponseBody
	public JsonResult updateBlocked(@RequestBody BlockedDyUserVo blockedDyUserVo) {
		User user = (User) SecurityUtils.getSubject().getSession().getAttribute("loginUserInfo");
		if (user == null || user.getId() == null) {
			return JsonResult.failure("参数错误");
		}
		if (blockedDyUserVo.getPublisherDyId() == null || "".equals(blockedDyUserVo.getPublisherDyId())) {
			return JsonResult.failure("参数无效");
		}
		DyuserBlocked dyuserBlocked = new DyuserBlocked();
		DyUser dyUser = dyUserService.findDyUserByDyId(blockedDyUserVo.getPublisherDyId());
		if (dyUser == null) {
			return JsonResult.failure("无效用户");
		}

		if (!blockedDyUserVo.getBlockedType().equals("0")) {
			Calendar nowAfter = Calendar.getInstance();
			if (blockedDyUserVo.getBlockedTimeType().equals("1")) {// 封禁1天
				nowAfter.set(Calendar.DAY_OF_YEAR, nowAfter.get(Calendar.DAY_OF_YEAR) + 1);
				dyuserBlocked.setBlockedEndtime(nowAfter.getTime());
			} else if (blockedDyUserVo.getBlockedTimeType().equals("2")) {// 封禁7天
				nowAfter.set(Calendar.DAY_OF_YEAR,nowAfter.get(Calendar.DAY_OF_YEAR) + 7);
				dyuserBlocked.setBlockedEndtime(nowAfter.getTime());
			} else if (blockedDyUserVo.getBlockedTimeType().equals("3")) {// 封禁30天
				nowAfter.set(Calendar.DAY_OF_YEAR,nowAfter.get(Calendar.DAY_OF_YEAR) + 30);
				dyuserBlocked.setBlockedEndtime(nowAfter.getTime());
			} else if (blockedDyUserVo.getBlockedTimeType().equals("4")) {// 封禁永久
				dyuserBlocked.setBlockedEndtime(nowAfter.getTime());
			}

			dyuserBlocked.setBlockedTimeType(blockedDyUserVo.getBlockedTimeType());
			dyuserBlocked.setBlockedType(blockedDyUserVo.getBlockedType());
			dyuserBlocked.setCreateTime(new Date());
			dyuserBlocked.setCreateUser(1L);
			dyuserBlocked.setDyContentId(blockedDyUserVo.getDyContentId());
			dyuserBlocked.setDyId(blockedDyUserVo.getPublisherDyId());
			blockedService.saveDyuserBlocked(dyuserBlocked);
			// 设置帖子为不通过
			contentExuserService.updateContentExuserBlocked(blockedDyUserVo.getPublisherDyId());
			// 设置评论为不通过
			commentExuserService.updateCommentExuserBlocked(blockedDyUserVo.getPublisherDyId());
			// 设置用户禁言
			dyUser.setUserStatus("2");
			dyUser.setStatusDeadline(dyuserBlocked.getBlockedEndtime());
			dyUserService.updateDyUser(dyUser);
			// 设置当前帖子 审核用户

		} else {
			dyuserBlocked.setBlockedTimeType(blockedDyUserVo.getBlockedTimeType());
			dyuserBlocked.setBlockedType(blockedDyUserVo.getBlockedType());
			dyuserBlocked.setCreateTime(new Date());
			dyuserBlocked.setCreateUser(Long.valueOf(user.getId()));
			dyuserBlocked.setDyContentId(blockedDyUserVo.getDyContentId());
			dyuserBlocked.setDyId(blockedDyUserVo.getPublisherDyId());
			blockedService.saveDyuserBlocked(dyuserBlocked);

			dyUserService.updateDyUser(dyUser);
		}
		return JsonResult.success();
	}

}
